const CLOUDBASEENV = 'sgb-8gqmg7kv48722f4e'
const cloudbaseApp = cloudbase.init({env:CLOUDBASEENV})
const cloudbaseAuth = cloudbaseApp.auth({persistence: "local"});//用户显式退出或更改密码之前的30天一直有效
const loginHtml = ['play-list','moxiang'] //需要进行登录的页面集合
//需要登录的页面
const loginHtmlPath={'moxiang':'../moxiang.html','play-list':'../play-list.html'}
//登录成功后要跳转的首页
const INDEXHTML = '../index-bootstarp.html'
//匿名登录
async function anonymouslogin(){
    // 应用初始化时检测是否登录
    const loginState = cloudbaseAuth.hasLoginState();
    if (loginState) {
        // 有登录状态
        console.log('登录了')
        const HREF = window.location.href
        const rootName = HREF.split('/')[HREF.split('/').length-1].split('.html')[0]
        if(cloudbaseAuth.loginType == 'ANONYMOUS'){ //匿名登录
            if(Object.keys(loginHtmlPath).includes(rootName)){
                if(rootName == 'play-list'){

                }else{
                    window.location.href ='../login/login.html?redirect=' + rootName
                }
            }
        }else if(cloudbaseAuth.loginType=='EMAIL'){ //邮件登录
            const notLogin = ['login','zhuze','resetPassWord']
            if(notLogin.includes(rootName)){
                window.location.href = INDEXHTML
            }
        }
    } else {
        // 没有登录
        console.log('没有登录')
        await cloudbaseAuth.anonymousAuthProvider().signIn();
    }
}
//注册邮箱
function zhuzeEmail(email,password){
    return cloudbaseApp.auth().signUpWithEmailAndPassword(email, password)
}
//使用邮箱登录
function loginEmail(email,password){
    return cloudbaseApp.auth().signInWithEmailAndPassword(email, password)
}
//使用邮箱重置密码
function resetEmailPassWord(email){
    return cloudbaseApp.auth().sendPasswordResetEmail(email)

}
//登出
function loginOut(){
    cloudbaseAuth.signOut();
}
